package my.Views;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import my.eightZOne.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class treasureView extends View {

    public treasureView(Context context, AttributeSet attrs) throws FileNotFoundException {

        super(context, attrs);
        // TODO Auto-generated constructor stub
        setFocusable(true);        //设置焦点调用系统监听
        flag_bmp = 0;
        read();
    }

    private Canvas canvasOut;                        //定义画布
    private final Paint paint = new Paint();        //定义画笔
    private int flag_bmp = 1;
    private float Sheight;        //屏幕宽度
    private float Swidth;        //屏幕高度
    private float HeightScale;        //屏幕宽度与预设比例
    private float WidthScale;        //屏幕高度与预设比例

    Bitmap rank = BitmapFactory.decodeResource(getResources(), R.drawable.treasure);        //引入背景图片

    int[] jifen = new int[100];            //积分排行榜记录

    //重写父类绘图函数
    @Override
    protected void onDraw(Canvas canvas) {
        canvasOut = canvas;

        DisplayMetrics dm = getResources().getDisplayMetrics();
        Swidth = (float) dm.widthPixels;
        Sheight = (float) dm.heightPixels;
        WidthScale = Swidth / 480;        //屏幕宽度与预设比例
        HeightScale = Sheight / 800;        //屏幕高度与预设比例

        int bgWidth = rank.getWidth();
        int bgHeight = rank.getHeight();
        Bitmap rankF = scaleImg(rank, WidthScale * 480 / bgWidth, HeightScale * 800 / bgHeight);

//        Bitmap rankF = scaleImg(rank, WidthScale, HeightScale);        //缩放后背景
        canvasOut.drawBitmap(rankF, 0, 0, paint);                    //绘制背景

        if (flag_bmp == 1) {
            //BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            //bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/MyZone/mypicture.jpg");
            Bitmap Fbmp = scaleImg(bmp, 0.05f, 0.05f);
            canvasOut.drawBitmap(Fbmp, 50, 350, paint);
        }
        //numDraw(jifen);		//绘制排行榜分数
    }

    //本地读取排行榜
    protected void read() throws FileNotFoundException {

        try {
            //---SD卡存储器---
            File sdCard = Environment.getExternalStorageDirectory();
            File directory = new File(sdCard.getAbsolutePath() + "/MyZone");
            directory.mkdirs();
            File file = new File(directory, "rank.txt");

            //读数据
            FileInputStream fIn = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fIn);

            //Toast.makeText(this.getContext(), "hello", Toast.LENGTH_SHORT).show();

            char[] inputBuffer = new char[1];
            int x = 0, tnum = 0;

            //String snum = String.copyValueOf(inputBuffer, 0, 1);
            //Toast.makeText(this.getContext(), "'"+snum+"'", Toast.LENGTH_SHORT).show();
            while (isr.read(inputBuffer) > 0) {
                String snum = String.copyValueOf(inputBuffer, 0, 1);
                if (snum.equals("\n")) {
                    jifen[x++] = tnum;
                    tnum = 0;
                } else {
                    tnum = tnum * 10 + Integer.parseInt(snum);
                }
            }
            //Toast.makeText(this.getContext(), "'"+jifen[0]+jifen[1]+jifen[2]+jifen[3]+jifen[4]+jifen[5]+jifen[6]+jifen[7]+"'", Toast.LENGTH_SHORT).show();
            //Toast.makeText(this.getContext(), "e01702026e68616f", Toast.LENGTH_SHORT).show();
        } catch (IOException ioe) {
            File sdCard = Environment.getExternalStorageDirectory();
            File directory = new File(sdCard.getAbsolutePath() + "/MyZone");
            directory.mkdirs();
            File file = new File(directory, "rank.txt");
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            ioe.printStackTrace();
        }

        postInvalidate();        //再次调用onDraw函数重绘画布
    }

    //显示分数
    protected void numDraw(int[] jifen) {

    }

    //缩放图片函数
    public Bitmap scaleImg(Bitmap bm, float scaW, float scaH) {
        // 获得图片的宽高		
        int width = bm.getWidth();
        int height = bm.getHeight();

        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaW, scaH);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }


//	public boolean onTouchEvent(MotionEvent event) {
//		if ((event.getAction() == MotionEvent.ACTION_DOWN)) {
//			String imageFilePath = Environment.getExternalStorageDirectory()
//					.getAbsolutePath() + "/MyZone/mypicture.jpg";
//			File imageFile = new File(imageFilePath);
//			Uri imageFileUri = Uri.fromFile(imageFile);
//			Intent i = new Intent(
//					MediaStore.ACTION_IMAGE_CAPTURE);
//			i.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
//			i.putExtra("camerasensortype", 1);
//			i.putExtra("needpath", true);
//			i.putExtra("needuri", true);
//			i.putExtra("camerasensortype", 1);
//			((Activity) treasureView.this.getContext()).startActivityForResult(i, 0);
//			// //////////////////////////////////////android.provider.
//			flag_bmp =1;
//			postInvalidate(); // 再次调用onDraw函数重绘画布
//		}
//		return true;
//	}
}
